from sandbox import align
from lib.myhmm import MarkovChain

import re
import chars
import util

datadir = '../data/'

def classify():
    grkl_file = open(datadir+'word_list.txt', 'rb')
    phonetic_file = open(datadir+'phonetic.txt', 'wb')
    orthographic_file = open(datadir+'orthographic.txt', 'wb')
    others_file = open(datadir+'others.txt', 'wb')
    contents = grkl_file.read()
    if contents.startswith('\xef\xbb\xbf'):
        contents = contents[3:] #strip BOM       
    contents = unicode(contents, 'utf-8')
    pattern = re.compile(r'(\w+) - (\w+) - (\d+) - ', re.UNICODE)
    #print repr(contents)
#    grkl_count = 0
    for line in contents.splitlines():
        #print repr(line)
        #print repr(line.split('-'))
        m = pattern.match(line)
        if m == None: continue
        grkl_word = m.group(1)
        greek_word = m.group(2)
        count = m.group(3)
        orthographic = 0
        phonetic = 0
        prev = None
        for c in grkl_word:
            if(prev):
                if ''.join([prev,c]) in chars.phonetic_grkl: phonetic+=4
                if ''.join([prev,c])  in chars.orthographic_grkl: orthographic+=3
            if c in chars.phonetic_grkl: phonetic+=1.1
            if c in chars.orthographic_grkl: orthographic+=1
                
            prev = c
        if abs(orthographic-phonetic)>=0.4:
            if orthographic>phonetic:
                orthographic_file.write(grkl_word)
                orthographic_file.write(' - ')
                orthographic_file.write(greek_word.encode('utf-8'))
                orthographic_file.write(' - ')
                orthographic_file.write(count)                
                orthographic_file.write(' - ')                
                orthographic_file.write('\r\n')
            else:
                phonetic_file.write(grkl_word)
                phonetic_file.write(' - ')
                phonetic_file.write(greek_word.encode('utf-8'))
                phonetic_file.write(' - ')
                phonetic_file.write(count)                
                phonetic_file.write(' - ')                
                phonetic_file.write('\r\n')             
        else:
            others_file.write(grkl_word)
            others_file.write(' - ')
            others_file.write(greek_word.encode('utf-8'))
            others_file.write(' - ')
            others_file.write(count)                
            others_file.write(' - ')                
            others_file.write('\r\n')
            
def reclassify():
    others_file = open('others.txt', 'rb')
    phonetic_file = open('phonetic2.txt', 'wb')
    orthographic_file = open('orthographic2.txt', 'wb')
    chain_phon = trainChain('phonetic.txt')
    chain_orth = trainChain('orthographic.txt')
    for line in others_file:
        word = line[:-2]
        seq = util.split_word_bigrams(word)
        if(chain_phon.probability(seq)>chain_orth.probability(seq)):
            phonetic_file.write(word)
            phonetic_file.write('\r\n')
        else:
            orthographic_file.write(word)
            orthographic_file.write('\r\n')
        
    
def trainChain(filename):
    file = open(filename, 'rb')
    sequences = []
    pattern = re.compile(r'(\w+) - (\w+) - (\d+) - ', re.UNICODE)    
    for line in file:
        m = pattern.match(line)
        if m == None: continue
        grkl_word = m.group(1)
#        word = line[:-2]
        seq = align.split_word_grkl(grkl_word)
        sequences.append((seq,1))
    chain = MarkovChain([],{},{})
    chain.train(sequences, True)
    return chain

if __name__ == '__main__':
    classify()
        
        
            
            
        
        
        
    